package com.crcc.common.util;

import com.crcc.common.constant.CommonConsts;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangcl
 * @version 1.0
 * @description: 时间工具类
 * @date 2021/4/7 14:30
 */
public class DateUtil {

    /**
     * @description: 根据时间戳获取特定时间（7天之内：n天前、n小时前、n分钟前，其余为年-月-日，排序方式：最早的在前）
     * @author wangcl
     * @date 2021/4/7 14:34
     * @version 1.0
     */
    public static String getWishTimeBy(Long timeStamp){
        //结果
        String resultStr = "";
        //当前时间戳
        Long nowTimeStamp = System.currentTimeMillis();
        //差值
        Long resTimeStamp = nowTimeStamp-timeStamp;
        //7天
        Long sevenDay = CommonConsts.SEVEN*CommonConsts.ONE_DAY;
        //1天
        Long oneDay = CommonConsts.ONE_DAY;
        //1小时
        Long oneHour = CommonConsts.ONE_HOUR;
        //1分钟
        Long oneMinute = CommonConsts.ONE_MINUTES;
        if (resTimeStamp <= sevenDay && resTimeStamp >= oneDay){
            resultStr = resTimeStamp/oneDay + "天前";
        }else if (resTimeStamp < oneDay && resTimeStamp >= oneHour){
            resultStr = resTimeStamp/oneHour + "小时前";
        }else if (resTimeStamp < oneHour && resTimeStamp >= oneMinute){
            resultStr = resTimeStamp/oneMinute + "分钟前";
        }else if(resTimeStamp < oneMinute){
            resultStr = "刚刚";
        }else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            resultStr = simpleDateFormat.format(timeStamp);
        }

        return resultStr;
    }



    // 获取昨天的时间格式字符串
    public static Date getYesterday(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * @description: 根据时间戳获取日期，如“3月1日”
     * @param timeStamp 时间戳
     * @param dateFormat 日期样式，如：yyyy-MM-dd
     * @author wangcl
     * @date 2021/4/15 11:10
     * @version 1.0
     */
    public static String getDateTimeBy(Long timeStamp,String dateFormat){
        //结果
        String resultStr = "";
        if(timeStamp == null){
            return resultStr;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        resultStr = simpleDateFormat.format(timeStamp);

        if (resultStr.startsWith(CommonConsts.ZERO + "")){
            resultStr = resultStr.substring(1);
        }
        return resultStr;
    }

    public static Date getDateTimeBy(Long timeStamp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(timeStamp);
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DAY_OF_MONTH, -1);
        return now.getTime();
    }

    public static String getDateBy(Long timeStamp){
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeStamp);
        Date date = cal.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String fmt = dateFormat.format(date);
        return fmt;
    }

    public static Date getDateByFangyi(Long timeStamp) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeStamp);
        Date date = cal.getTime();
        DateFormat dateFormat = new SimpleDateFormat("yyyy/M/d");

        String fmt = dateFormat.format(date);
        return dateFormat.parse(fmt);
    }

    public static String getDateMilliBy(Long timeStamp){
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeStamp);
        Date date = cal.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        String fmt = dateFormat.format(date);
        return fmt;
    }

    /**
     * @description: string类型日期格式化，如“20210916”格式化为"2021-09-16"
     * @param timeStamp 时间串
     * @author zhengxy
     * @date 2021/9/16 09:41
     * @version 1.0
     */
    public static String getFormatDate(String timeStamp){
        Date parse = null;
        String dateString = "";
        try {
            parse = new SimpleDateFormat("yyyyMMdd").parse(timeStamp);
            dateString = new SimpleDateFormat("yyyy-MM-dd").format(parse);
        } catch (ParseException e) {
            dateString=null;
        }

        return dateString;
    }

    /**
     * @description: 根据时间戳获取7天日期list(签到使用)
     * @param timeStamp 时间戳
     * @param dateFormat 日期样式，如：yyyy-MM-dd
     * @param signDay 连续签到天数
     * @author wangcl
     * @date 2021/4/15 11:36
     * @version 1.0
     */
    public static List<HashMap> getSignTimeListBy(Long timeStamp, String dateFormat, Integer signDay){

        //结果
        List<HashMap> resList = new ArrayList();

        //获得起始时间点
        Long startTime = 0L;
        if (signDay == CommonConsts.ZERO){
            startTime = timeStamp - (signDay*CommonConsts.ONE_DAY);
        }else{
            startTime = timeStamp - ((signDay-CommonConsts.ONE)*CommonConsts.ONE_DAY);
        }
        for (int i = 0; i < CommonConsts.SEVEN; i++) {
            HashMap hashMap = new HashMap();
            String resultStr = "";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
            resultStr = simpleDateFormat.format(startTime);
            startTime = startTime + CommonConsts.ONE_DAY;
            if (resultStr.startsWith(CommonConsts.ZERO + "")){
                resultStr = resultStr.substring(CommonConsts.ONE);
            }

            if (signDay > CommonConsts.ZERO){
                hashMap.put("dayTime",resultStr);
                hashMap.put("dayStatus",CommonConsts.ONE);
                signDay--;
            }else {
                hashMap.put("dayTime",resultStr);
                hashMap.put("dayStatus",CommonConsts.ZERO);
            }

            resList.add(hashMap);
        }

        return resList;
    }

    /**
     * @description: 判断年份是否是闰年
     * @param year 年份
     * @author wangcl
     * @date 2021/4/19 15:48
     * @version 1.0
     */
    public static boolean isLeapYear(Integer year) {
        return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
	}

    public static String formatDate(Date date, String formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter, Locale.CHINESE);
        return sdf.format(date);
    }

    /**
     * 获得当日0点时间戳
     * @return
     */
    public static Long getTodayZeroPointTimestamps(){
        Long currentTimestamps=System.currentTimeMillis();
        return currentTimestamps-(currentTimestamps+(CommonConsts.EIGHT*CommonConsts.ONE_HOUR))%CommonConsts.ONE_DAY;
    }
    // 获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    // 获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
                0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
                59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取某个日期的开始时间(Long型)
    public static Long getDayStartTimeToLong(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 00,
                00, 00);
        calendar.set(Calendar.MILLISECOND, 001);
        return calendar.getTimeInMillis();
    }

    // 获取某个日期的结束时间(Long型)
    public static Long getDayEndTimeToLong(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
                59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    // 获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    // 获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获得时间串对应的13位时间戳
     * @param timeStr 时间串，例如：2021-07-21
     * @param dateFormat 日期样式，如：yyyy-MM-dd HH:mm:ss
     * @param timeType 0.正常时间；1.开始时间；2.结束时间
     * @return
     */
    public static Long getTimeStampBy(String timeStr,String dateFormat,int timeType){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        Long time = null;
        Date date = null;
        try {
            if(CommonConsts.ZERO == timeType){
                date = simpleDateFormat.parse(timeStr);
            }else if(CommonConsts.ONE == timeType){
                date = simpleDateFormat.parse(timeStr + " 00:00:00");
            }else if(CommonConsts.TWO == timeType){
                date = simpleDateFormat.parse(timeStr + " 23:59:59");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        time = date.getTime();
        return time;
    }
    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0) //闰年
                {
                    timeDistance += 366;
                }
                else //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else //不同年
        {
            return day2-day1;
        }
    }

    /**
     * 获取最近30天或一年的日期list
     * @param chooseType 1.最近30天；2.最近一年
     * @return
     */
    public static LinkedList<String> getTimeListBy(int chooseType){

        LinkedList<String> list = new LinkedList<>();
        Calendar c = Calendar.getInstance();
        if (CommonConsts.ONE == chooseType){
            SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
            for (int i = 29; i >= 0; i--) {
                c.setTime(new Date());
                c.add(Calendar.DATE, -i);
                Date d = c.getTime();
                String day = format.format(d);
                list.add(day);
            }
        }else if (CommonConsts.TWO == chooseType){
            SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_YEAR);
            for (int i = 11; i >= 0; i--) {
                c.setTime(new Date());
                c.add(Calendar.MONTH, -i);
                Date m3 = c.getTime();
                String mon3 = format.format(m3);
                list.add(mon3);
            }
        }else if (CommonConsts. THREE == chooseType){
            SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
            for (int i = 11; i >= 0; i--) {
                c.setTime(new Date());
                c.add(Calendar.MONTH, -i);
                Date m3 = c.getTime();
                String mon3 = format.format(m3);
                list.add(mon3);
            }
        }

        return list;
    }

    /**
     * 获取最近30天或一年的日期list
     * @param chooseType 1.最近30天；2.最近一年
     * @return
     */
    public static LinkedList<String> getTimeListBy(int chooseType,Date date){

        LinkedList<String> list = new LinkedList<>();
        Calendar c = Calendar.getInstance();
        if (CommonConsts.ONE == chooseType){
            SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
            for (int i = 29; i >= 0; i--) {
                c.setTime(date);
                c.add(Calendar.DATE, -i);
                Date d = c.getTime();
                String day = format.format(d);
                list.add(day);
            }
        }else if (CommonConsts.TWO == chooseType){
            SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_YEAR);
            for (int i = 11; i >= 0; i--) {
                c.setTime(date);
                c.add(Calendar.MONTH, -i);
                Date m3 = c.getTime();
                String mon3 = format.format(m3);
                list.add(mon3);
            }
        }else if (CommonConsts. THREE == chooseType){
            SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
            for (int i = 11; i >= 0; i--) {
                c.setTime(date);
                c.add(Calendar.MONTH, -i);
                Date m3 = c.getTime();
                String mon3 = format.format(m3);
                list.add(mon3);
            }
        }

        return list;
    }



    /**
     * 获取最近30天或一年的日期list
     * @return
     */
    public static LinkedList<String> getTimeListByYear(Date date){

        LinkedList<String> list = new LinkedList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        aCalendar.add(Calendar.MONTH, -1);
        int days = (int) ((c.getTime().getTime() - aCalendar.getTime().getTime()) / (1000*3600*24));
        SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
        for (int i = days-1; i >= 0; i--) {
            c.setTime(date);
            c.add(Calendar.DATE, -i);
            Date d = c.getTime();
            String day = format.format(d);
            list.add(day);
        }
        return list;
    }

    /**
     * 获取最近30天或一年的日期list
     * @return
     */
    public static LinkedList<String> getTimeListByYear(Date date,int days){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        LinkedList<String> list = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
        for (int i = days-1; i >= 0; i--) {
            c.setTime(date);
            c.add(Calendar.DATE, -i);
            Date d = c.getTime();
            String day = format.format(d);
            list.add(day);
        }
        return list;
    }

    /**
     * 获取最近30天或一年的日期list
     * @return
     */
    public static LinkedList<String> getTimeListByDay(Date date,int days){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        LinkedList<String> list = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
        for (int i = days-1; i >= 0; i--) {
            c.setTime(date);
            c.add(Calendar.DATE, -i);
            Date d = c.getTime();
            String day = format.format(d);
            list.add(day.substring(8, 10));
        }
        return list;
    }

    public static LinkedList<String> getTimeListByDay(Date date)throws ParseException{

        LinkedList<String> list = new LinkedList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        aCalendar.add(Calendar.MONTH, -1);
        int days = (int) ((c.getTime().getTime() - aCalendar.getTime().getTime()) / (1000*3600*24));
        SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
        for (int i = days-1; i >= 0; i--) {
            c.setTime(date);
            c.add(Calendar.DATE, -i);
            Date d = c.getTime();
            String day = format.format(d);
            list.add(day.substring(8, 10));
        }
        return list;
    }

    public static LinkedList<String> getTimeListByYearDay(Date date)throws ParseException{

        LinkedList<String> list = new LinkedList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        aCalendar.add(Calendar.MONTH, -1);
        int days = (int) ((c.getTime().getTime() - aCalendar.getTime().getTime()) / (1000*3600*24));
        SimpleDateFormat format = new SimpleDateFormat(CommonConsts.DATE_FORMAT_SIMPLE);
        for (int i = days-1; i >= 0; i--) {
            c.setTime(date);
            c.add(Calendar.DATE, -i);
            Date d = c.getTime();
            String day = format.format(d);
            list.add(day);
        }
        return list;
    }
    public static LinkedList<String> getTimeListYear(int yearParam,int monthParam) {
        LinkedList<String> list = new LinkedList<String>();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,monthParam,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) +1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = year +"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = year +"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = year +"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = year +"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }



    /**
     * 计算工龄：不满半年为0.5,满半年为1
     * @param timeStamp 毫秒级时间戳
     * @return
     */
    public static float getWorkTime(Long timeStamp){

        float res = 0;

        if (Long.valueOf(CommonConsts.ZERO).equals(timeStamp)){
            //入参为0
            return res;
        }else {
            int tem = (int)(timeStamp/CommonConsts.HALF_YEAR);
            if (0 == tem){
                //不满半年
                res = 1;
            }else {
                //满半年
                Long yu = timeStamp%CommonConsts.HALF_YEAR;
                if (!Long.valueOf(CommonConsts.ZERO).equals(yu)){
                    //有余数
                    res = tem + 1;
                }else {
                    //无余数
                    res = tem;
                }
            }
        }

        return res*0.5f;
    }

    public static boolean isFirstDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1);
        return calendar.get(Calendar.DAY_OF_MONTH) == 2;
    }

    //获取指定月份第一天
    public static Long getFisrtDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return getTimeStampBy(lastDayOfMonth,CommonConsts.DATE_FORMAT,CommonConsts.ONE);
    }
    //获取指定月份最后一天
    public static Long getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return getTimeStampBy(lastDayOfMonth,CommonConsts.DATE_FORMAT,CommonConsts.TWO);
    }
    //获取指定上个月份最后一天
    public static Long getLastDayOfLastMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-2);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return getTimeStampBy(lastDayOfMonth,CommonConsts.DATE_FORMAT,CommonConsts.TWO);
    }
    public static Long getMonthStart()
    {
        //获取上个月的第一天
        Calendar cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        //例如今天 2021/8/11 sout后会输出：2021/7/1 00:00:00
        return cal_1.getTime().getTime();
    }
    public static Long getMonthEnd()
    {
        //获取上个月的最后一天
        Calendar cal_2 = Calendar.getInstance();
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        return cal_2.getTime().getTime();
    }
    /**
     * @param date 时间
     * @return 汉字年月日
     */
    public static String formatStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        String[] split = format.split("-");
        //2020-7-18 ==> 2002年7月18日
        String year = split[0];
        String month = split[1];
        String day = split[2];
        String formatStr = year + "年" + month + "月" + day + "日";

        return formatStr;
    }

    /**
     * 根据给定日期，获取过去第几天的日期
     * @Author xmj
     * @Description 根据给定日期，获取过去第几天的日期
     * @Date 10:36 2023/3/8
     * @param past 过去天数
     * @param date 给定日期
     * @Return java.lang.String
     */
    public static Date getPastDate(int past,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        return sdf.format(today);
        return today;
    }

    /**
     * 生成size数量的随机时间，位于(start,end)范围内 可控制顺序
     * @param start 开始时间
     * @param end 结束时间
     * @param size 生成时间个数
     * @param order 结果排序：-1时间倒叙，0 乱序，1时间正序
     * @return List<String>
     * @throws Exception
     */
    public static List<String> randomDate(String start, String end, int size, int order) throws Exception {

        //当前日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = simpleDateFormat.format(new Date());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(dateTime + " " + start);
        Date endTime = sdf.parse(dateTime + " " + end);

        Random random = new Random();
        List<String> dates = random.longs(size, startTime.getTime(), endTime.getTime()).mapToObj(t -> sdf.format(new Date(t))).collect(Collectors.toList());

        dates.sort((t1,t2)->{
            return t1.compareTo(t2) * order;
        });

        return dates;
    }
    public static String getTimeToDate(String date, int month) throws Exception {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date startdate = simpleDateFormat.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startdate);
        calendar.add(Calendar.MONTH, month);
        startdate = calendar.getTime();
        String defaultStartDate = new SimpleDateFormat("yyyy-MM").format(startdate);
        return defaultStartDate;
    }




    public static void main(String[] args) throws Exception {
        System.out.println(Integer.valueOf("09"));
        System.out.println(Integer.valueOf("11"));
//        List<String> dates = randomDate("08:54:03", "10:55:24", 10,0);
//        dates.forEach(t -> System.out.println(t));

//
//        System.out.println(getMonthStart());
//        System.out.println(formatStr(new Date(1642262399000L)));
////        System.out.println(getOpenDayBy(1614225668000l));
////        System.out.println(getDateTimeBy(1618815700546l,CommonConsts.SIGN_DATE_FORMAT));
////        int day =Math.toIntExact((1651679999000l - 1650988800000l + 1000000) / (60 * 60 * 24 * 1000));
////        Calendar aCalendar = Calendar.getInstance();
////        int year = aCalendar.get(Calendar.YEAR);
//        System.out.println(getFisrtDayOfMonth(2022,06)); //年份getNowMonth());
//        System.out.println(getLastDayOfMonth(2022,06)); //年份getNowMonth());
////        System.out.println(getYesterday(new Date()));
//        //System.out.println(getTimeListBy(1,getDateTimeBy(1647839520190l)));
//        long time1 = 1662393600;
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            String date = sdf.format(new Date(1662393600*1000L));
//            System.out.println(date);
//            System.out.println(getDayEndTimeToLong(new Date(1662393600*1000L)));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

}
